package com.controller.employee;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pojo.Employee;
import com.pojo.Privilege;
import com.pojo.User;
import com.service.employeeservice.EmployeeService;

@Controller
public class EmployeeController {
     @Autowired
     private EmployeeService employeeService;
  
     @RequestMapping("/logincf")
 	public String index() {
 		
    	 return "logincf";
 	}
     
     
   //登录
 	@RequestMapping("/loginIncf")
 	public String loginIncf(Employee employee, String slidBlock,HttpServletRequest request, HttpServletResponse response, HttpSession session){
 		if ("go".equals(slidBlock)) {
 			Employee loginEmployee = employeeService.login(employee);
 			if (loginEmployee!=null) {
 				session.setAttribute("loginEmployee", loginEmployee);
 				Cookie userNameCookie = new Cookie("loginEmployeeusername", loginEmployee.getEmployeeusername());
				Cookie passwordCookie = new Cookie("loginEmployeePassword",loginEmployee.getEmployeepassword() );
				userNameCookie.setMaxAge(60 * 60);
				userNameCookie.setPath("/");
				passwordCookie.setMaxAge(60 * 60);
				passwordCookie.setPath("/");
				response.addCookie(userNameCookie);
				response.addCookie(passwordCookie);
 				
				List<Privilege> privileges = employeeService.getPrivilege(loginEmployee.getEmployeeid());
				session.setAttribute("privileges", privileges);
				return "indexcf";
				       
			} else {
				return "logincf";
			}
 			
 		} else {
 			return "logincf";
		}
 	}; 
     
     
	@RequestMapping("/indexcf")
	public String indexcf(HttpServletRequest req,HttpServletResponse response,HttpSession session) {
		Employee employee = (Employee) session.getAttribute("loginEmployee");
		if(employee==null){
			return "redirect:/logincf";
		}
		return "indexcf";
	}
	
	@RequestMapping("/indexcf2")
	public String indexcf2(HttpServletRequest req,HttpServletResponse response,HttpSession session) {

		return "indexcf";
		
	}
 	
	//欢迎页面
		@RequestMapping("/welcomecf")
		public String welcomecf() {
			return "welcomecf";
		}
		
		
		//注销，切换账号
		@RequestMapping("/login_out")
		public String logout(HttpServletRequest request, HttpServletResponse response) {
			Employee loginEmployee = (Employee) request.getSession().getAttribute("loginEmployee");
			// 删除登录cookie
			Cookie userNameCookie = new Cookie("loginEmployeeusername", loginEmployee.getEmployeeusername());
			Cookie passwordCookie = new Cookie("loginEmployeePassword", loginEmployee.getEmployeepassword());
			userNameCookie.setMaxAge(0);
			userNameCookie.setPath("/");
			passwordCookie.setMaxAge(0);
			passwordCookie.setPath("/");
			response.addCookie(userNameCookie);
			response.addCookie(passwordCookie);
			request.getSession().removeAttribute("loginEmployee");
			return "redirect:/logincf.html";
		}
		
     
}
